package interfaces;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.toedter.calendar.JDateChooser;

import controller.SleepChartController;


public class SleepChartPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private SleepChartController controller;
	private SleepChart chart;
	private JDateChooser start = new JDateChooser();
	private JDateChooser end = new JDateChooser();
	
	public SleepChartPanel(SleepChartController controller) {
		super(new BorderLayout());
		this.controller = controller;
		initComponents();
	}
	private void initComponents() {
		add(getNorthPanel(), BorderLayout.NORTH);
		add(getCenterPanel(), BorderLayout.CENTER);
		
	}
	private JPanel getCenterPanel() {
		JPanel panel = new JPanel();
		chart = new SleepChart(controller.getData());
		panel.add(chart);
		return panel;
	}
	private JPanel getNorthPanel() {	
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		JPanel panel1 = new JPanel(new GridBagLayout());
		panel1.add(new JLabel("Начало интервала"),  new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 5), 0, 0));
		panel1.add(start,  new GridBagConstraints(1, 0, 2, 1, 1.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 10, 0, 5), 0, 0));
		panel1.add(new JLabel("Конец интервала"),  new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 5), 0, 0));
		panel1.add(end,  new GridBagConstraints(1, 1, 2, 1, 1.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 10, 0, 5), 0, 0));
		JButton button = new JButton("Отобразить");
		panel1.add(button,  new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 5), 0, 0));
		
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				chart.setData(controller.getDataInterval(start.getDate(), end.getDate()));
			}
		});
		
		panel.add(panel1);
		return panel;
	}
}
